package br.com.sovi.mobile.j2me;

import br.com.sovi.mobile.j2me.gps.LocationRecord;
import br.com.sovi.mobile.j2me.gps.UpdateListener;
import br.com.sovi.mobile.j2me.stream.RecordPointStream;
import br.com.sovi.mobile.j2me.stream.StreamListener;
import java.util.Date;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.QualifiedCoordinates;

/**
 * @author João Galli
 */
public class SoviMidlet extends MIDlet implements CommandListener, ItemCommandListener {

    private boolean midletPaused = false;

    private static SoviMidlet instance;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form mainForm;
    private TextField latitudeField;
    private TextField idField;
    private TextField longitudeField;
    private StringItem statusField;
    private Form configHttpForm;
    private ChoiceGroup opcoesGroup;
    private TextField urlField;
    private TextField textField;
    private Form configGpsForm;
    private TextField horizontalAccuracyField;
    private TextField intervaloField;
    private TextField preferredResponseTimeField;
    private TextField verticalAccuracyField;
    private ChoiceGroup opcoesGroup1;
    private ChoiceGroup consumoEnergiaGroup;
    private Command enviarPosicaoServidorCommand;
    private Command startGPSCommand;
    private Command exitCommand;
    private Command configHttpCommand;
    private Command backCommand;
    private Command configGpsCommand;
    private Command backCommand1;
    private Command playToneCommand;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The SoviMidlet constructor.
     */
    public SoviMidlet() {
        SoviMidlet.instance = this;
        Log.setLog(new Log() {

            public void log(String message) {
                statusField.setText(message);
            }
        });
    }

    public static SoviMidlet get() {
        return instance;
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
        opcoesGroup = new ChoiceGroup("Op\u00E7\u00F5es:", Choice.MULTIPLE);//GEN-BEGIN:|0-initialize|1|0-postInitialize
        opcoesGroup.append("Enviar para servidor", null);
        opcoesGroup.setSelectedFlags(new boolean[] { false });
        opcoesGroup.setFont(0, null);
        urlField = new TextField("URL", "http://server.sovi.com.br/sovi-server/m/p/post/add", 300, TextField.ANY);
        horizontalAccuracyField = new TextField("Horizontal Accuracy", "30", 32, TextField.NUMERIC);
        intervaloField = new TextField("Intervalo de Atualiza\u00E7\u00E3o (segundos)", "30", 32, TextField.NUMERIC);
        preferredResponseTimeField = new TextField("Preferred Response Time", "2000", 32, TextField.NUMERIC);
        verticalAccuracyField = new TextField("Vertical Accuracy", "30", 32, TextField.NUMERIC);
        opcoesGroup1 = new ChoiceGroup("Op\u00E7\u00F5es", Choice.MULTIPLE);
        opcoesGroup1.append("Permitir cobran\u00E7a", null);
        opcoesGroup1.append("Buscar Velocidade e Rota", null);
        opcoesGroup1.append("Altitude", null);
        opcoesGroup1.append("Informa\u00E7\u00E3o do Endere\u00E7o", null);
        opcoesGroup1.setSelectedFlags(new boolean[] { false, false, false, false });
        opcoesGroup1.setFont(0, null);
        opcoesGroup1.setFont(1, null);
        opcoesGroup1.setFont(2, null);
        opcoesGroup1.setFont(3, null);
        consumoEnergiaGroup = new ChoiceGroup("Consumo de Energia", Choice.POPUP);
        consumoEnergiaGroup.append("Baixo", null);
        consumoEnergiaGroup.append("M\u00E9dio", null);
        consumoEnergiaGroup.append("Alto", null);
        consumoEnergiaGroup.setSelectedFlags(new boolean[] { false, false, false });
        consumoEnergiaGroup.setFont(0, null);
        consumoEnergiaGroup.setFont(1, null);
        consumoEnergiaGroup.setFont(2, null);//GEN-END:|0-initialize|1|0-postInitialize
    // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getMainForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
    // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
    // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == configGpsForm) {//GEN-BEGIN:|7-commandAction|1|55-preAction
            if (command == backCommand1) {//GEN-END:|7-commandAction|1|55-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainForm());//GEN-LINE:|7-commandAction|2|55-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|41-preAction
        } else if (displayable == configHttpForm) {
            if (command == backCommand) {//GEN-END:|7-commandAction|3|41-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainForm());//GEN-LINE:|7-commandAction|4|41-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|52-preAction
        } else if (displayable == mainForm) {
            if (command == configGpsCommand) {//GEN-END:|7-commandAction|5|52-preAction
                // write pre-action user code here
                switchDisplayable(null, getConfigGpsForm());//GEN-LINE:|7-commandAction|6|52-postAction
            // write post-action user code here
            } else if (command == configHttpCommand) {//GEN-LINE:|7-commandAction|7|38-preAction
                // write pre-action user code here
                switchDisplayable(null, getConfigHttpForm());//GEN-LINE:|7-commandAction|8|38-postAction
            // write post-action user code here
            } else if (command == enviarPosicaoServidorCommand) {//GEN-LINE:|7-commandAction|9|27-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|10|27-postAction

                //System.out.println("Posições capturadas: " + .size());

                new RecordPointStream(gPSStreamListener, urlField.getString(),
                        idField.getString(), LocationRecord.getById(idField.getString())).start();

            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|11|31-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|12|31-postAction
            // write post-action user code here
            } else if (command == startGPSCommand) {//GEN-LINE:|7-commandAction|13|29-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|14|29-postAction
                new Thread() {

                    public void run() {
                        startGPS();
                    }
                }.start();

            }//GEN-BEGIN:|7-commandAction|15|7-postCommandAction
        }//GEN-END:|7-commandAction|15|7-postCommandAction
    // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|16|
    //</editor-fold>//GEN-END:|7-commandAction|16|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: mainForm ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of mainForm component.
     * @return the initialized component instance
     */
    public Form getMainForm() {
        if (mainForm == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            mainForm = new Form("Main Form", new Item[] { getIdField(), getLatitudeField(), getLongitudeField(), getStatusField() });//GEN-BEGIN:|18-getter|1|18-postInit
            mainForm.addCommand(getEnviarPosicaoServidorCommand());
            mainForm.addCommand(getStartGPSCommand());
            mainForm.addCommand(getExitCommand());
            mainForm.addCommand(getConfigHttpCommand());
            mainForm.addCommand(getConfigGpsCommand());
            mainForm.setCommandListener(this);//GEN-END:|18-getter|1|18-postInit
        // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return mainForm;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: idField ">//GEN-BEGIN:|20-getter|0|20-preInit
    /**
     * Returns an initiliazed instance of idField component.
     * @return the initialized component instance
     */
    public TextField getIdField() {
        if (idField == null) {//GEN-END:|20-getter|0|20-preInit
            // write pre-init user code here
            idField = new TextField("ID", "1", 32, TextField.ANY);//GEN-LINE:|20-getter|1|20-postInit
        // write post-init user code here
        }//GEN-BEGIN:|20-getter|2|
        return idField;
    }
    //</editor-fold>//GEN-END:|20-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: latitudeField ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initiliazed instance of latitudeField component.
     * @return the initialized component instance
     */
    public TextField getLatitudeField() {
        if (latitudeField == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            latitudeField = new TextField("Latitude", "0", 32, TextField.ANY);//GEN-LINE:|21-getter|1|21-postInit
        // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return latitudeField;
    }
    //</editor-fold>//GEN-END:|21-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: longitudeField ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of longitudeField component.
     * @return the initialized component instance
     */
    public TextField getLongitudeField() {
        if (longitudeField == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            longitudeField = new TextField("Longitude", "0", 32, TextField.ANY);//GEN-LINE:|22-getter|1|22-postInit
        // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return longitudeField;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: statusField ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of statusField component.
     * @return the initialized component instance
     */
    public StringItem getStatusField() {
        if (statusField == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            statusField = new StringItem("Status", null);//GEN-LINE:|24-getter|1|24-postInit
        // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return statusField;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: enviarPosicaoServidorCommand ">//GEN-BEGIN:|26-getter|0|26-preInit
    /**
     * Returns an initiliazed instance of enviarPosicaoServidorCommand component.
     * @return the initialized component instance
     */
    public Command getEnviarPosicaoServidorCommand() {
        if (enviarPosicaoServidorCommand == null) {//GEN-END:|26-getter|0|26-preInit
            // write pre-init user code here
            enviarPosicaoServidorCommand = new Command("Enviar", "Enviar ao Servidor", Command.SCREEN, 0);//GEN-LINE:|26-getter|1|26-postInit
        // write post-init user code here
        }//GEN-BEGIN:|26-getter|2|
        return enviarPosicaoServidorCommand;
    }
    //</editor-fold>//GEN-END:|26-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: startGPSCommand ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of startGPSCommand component.
     * @return the initialized component instance
     */
    public Command getStartGPSCommand() {
        if (startGPSCommand == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            startGPSCommand = new Command("GPS", "Iniciar GPS", Command.SCREEN, 0);//GEN-LINE:|28-getter|1|28-postInit
        // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return startGPSCommand;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            exitCommand = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|30-getter|1|30-postInit
        // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: configHttpForm ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of configHttpForm component.
     * @return the initialized component instance
     */
    public Form getConfigHttpForm() {
        if (configHttpForm == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            configHttpForm = new Form("Configura\u00E7\u00E3o GPRS", new Item[] { urlField, opcoesGroup, getTextField() });//GEN-BEGIN:|33-getter|1|33-postInit
            configHttpForm.addCommand(getBackCommand());
            configHttpForm.setCommandListener(this);//GEN-END:|33-getter|1|33-postInit
        // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return configHttpForm;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: configHttpCommand ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of configHttpCommand component.
     * @return the initialized component instance
     */
    public Command getConfigHttpCommand() {
        if (configHttpCommand == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            configHttpCommand = new Command("Configurar", Command.SCREEN, 0);//GEN-LINE:|37-getter|1|37-postInit
        // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return configHttpCommand;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            backCommand = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|40-getter|1|40-postInit
        // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: configGpsForm ">//GEN-BEGIN:|46-getter|0|46-preInit
    /**
     * Returns an initiliazed instance of configGpsForm component.
     * @return the initialized component instance
     */
    public Form getConfigGpsForm() {
        if (configGpsForm == null) {//GEN-END:|46-getter|0|46-preInit
            // write pre-init user code here
            configGpsForm = new Form("Configura\u00E7\u00E3o GPS", new Item[] { intervaloField, horizontalAccuracyField, verticalAccuracyField, preferredResponseTimeField, consumoEnergiaGroup, opcoesGroup1 });//GEN-BEGIN:|46-getter|1|46-postInit
            configGpsForm.addCommand(getBackCommand1());
            configGpsForm.setCommandListener(this);//GEN-END:|46-getter|1|46-postInit
        // write post-init user code here
        }//GEN-BEGIN:|46-getter|2|
        return configGpsForm;
    }
    //</editor-fold>//GEN-END:|46-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: configGpsCommand ">//GEN-BEGIN:|51-getter|0|51-preInit
    /**
     * Returns an initiliazed instance of configGpsCommand component.
     * @return the initialized component instance
     */
    public Command getConfigGpsCommand() {
        if (configGpsCommand == null) {//GEN-END:|51-getter|0|51-preInit
            // write pre-init user code here
            configGpsCommand = new Command("Config. GPS", "Configurar GPS", Command.SCREEN, 0);//GEN-LINE:|51-getter|1|51-postInit
        // write post-init user code here
        }//GEN-BEGIN:|51-getter|2|
        return configGpsCommand;
    }
    //</editor-fold>//GEN-END:|51-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|54-getter|0|54-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|54-getter|0|54-preInit
            // write pre-init user code here
            backCommand1 = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|54-getter|1|54-postInit
        // write post-init user code here
        }//GEN-BEGIN:|54-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|54-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Items ">//GEN-BEGIN:|8-itemCommandAction|0|8-preItemCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular item.
     * @param command the Command that was invoked
     * @param displayable the Item where the command was invoked
     */
    public void commandAction(Command command, Item item) {//GEN-END:|8-itemCommandAction|0|8-preItemCommandAction
        // write pre-action user code here
        if (item == textField) {//GEN-BEGIN:|8-itemCommandAction|1|68-preAction
            if (command == playToneCommand) {//GEN-END:|8-itemCommandAction|1|68-preAction
                this.getDisplay().flashBacklight(1000);
                this.getDisplay().vibrate(1000);
                // write pre-action user code here
                switchDisplayable(null, getConfigHttpForm());//GEN-LINE:|8-itemCommandAction|2|68-postAction
            // write post-action user code here
            }//GEN-BEGIN:|8-itemCommandAction|3|8-postItemCommandAction
        }//GEN-END:|8-itemCommandAction|3|8-postItemCommandAction
    // write post-action user code here
    }//GEN-BEGIN:|8-itemCommandAction|4|
    //</editor-fold>//GEN-END:|8-itemCommandAction|4|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField ">//GEN-BEGIN:|66-getter|0|66-preInit
    /**
     * Returns an initiliazed instance of textField component.
     * @return the initialized component instance
     */
    public TextField getTextField() {
        if (textField == null) {//GEN-END:|66-getter|0|66-preInit
            // write pre-init user code here
            textField = new TextField("Tom", "10", 32, TextField.ANY);//GEN-BEGIN:|66-getter|1|66-postInit
            textField.addCommand(getPlayToneCommand());
            textField.setItemCommandListener(this);//GEN-END:|66-getter|1|66-postInit
        // write post-init user code here
        }//GEN-BEGIN:|66-getter|2|
        return textField;
    }
    //</editor-fold>//GEN-END:|66-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: playToneCommand ">//GEN-BEGIN:|67-getter|0|67-preInit
    /**
     * Returns an initiliazed instance of playToneCommand component.
     * @return the initialized component instance
     */
    public Command getPlayToneCommand() {
        if (playToneCommand == null) {//GEN-END:|67-getter|0|67-preInit
            // write pre-init user code here
            playToneCommand = new Command("Play Tone", Command.ITEM, 0);//GEN-LINE:|67-getter|1|67-postInit
        // write post-init user code here
        }//GEN-BEGIN:|67-getter|2|
        return playToneCommand;
    }
    //</editor-fold>//GEN-END:|67-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    private void startGPS() {
        Log.getLog().log("Starting GPS");

        LocationManager.startGPS(new UpdateListenerImpl(),
                Integer.parseInt(intervaloField.getString()), getCriteria());
    }

    private Criteria getCriteria() {
        Criteria c = new Criteria();
        c.setHorizontalAccuracy(Integer.parseInt(horizontalAccuracyField.getString()));
        c.setVerticalAccuracy(Integer.parseInt(verticalAccuracyField.getString()));

        return c;
    }

    public class UpdateListenerImpl implements UpdateListener {

        public void updated(Location update) {
            try {
                if (update != null) {
                    QualifiedCoordinates qc = update.getQualifiedCoordinates();

                    if (qc != null) {
                        latitudeField.setString(Double.toString(qc.getLatitude()));
                        longitudeField.setString(Double.toString(qc.getLongitude()));

                        System.out.println("enviando url: " + isEnviaURL());
                        if (isEnviaURL()) {
                            new RecordPointStream(gPSStreamListener, urlField.getString(),
                                    idField.getString(), qc.getLongitude(), qc.getLatitude()).start();
                        } else {
                            LocationRecord.add(idField.getString(), new Date(), qc.getLatitude(), qc.getLongitude());
                        }
                    }
                }
            } catch (Throwable t) {
                Log.getLog().log("Erro: " + t.getClass().getName() + " > " + t.getMessage());
            }
        }
    }

    public boolean isEnviaURL() {
        boolean[] selected = new boolean[]{false};

        opcoesGroup.getSelectedFlags(selected);

        return selected[0];
    }
    private GPSStreamListener gPSStreamListener = new GPSStreamListener();

    private class GPSStreamListener implements StreamListener {

        public void start() {
            Log.getLog().log("Started sending Location.");
        }

        public void onError(String exceptionMessage, Throwable throwable) {
            Log.getLog().log("Error URL: " + exceptionMessage + " > ");
            if (throwable != null) {
                throwable.printStackTrace();
            }
        }

        public void success() {
            Log.getLog().log("Location SENT.");
        }
    }
}
